package com.redwine.app;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.redwine.app.net.responses.RedwineDetailResponse;
import com.redwine.app.net.responses.SendMsgEvent;
import com.redwine.app.utils.Utils;

import de.greenrobot.event.EventBus;


/**
 * 申请供应商第二步
 * 作者：gassion on 17/8/7 22:46
 * 最可怕的敌人，就是没有坚强的信念。
 */

public class ApplyForSupplierNextActivity extends BaseActivity {

    @ViewInject(R.id.header_title)
    private TextView headerTitle;
    @ViewInject(R.id.layout_back)
    private LinearLayout layoutBack;
    public static final String PUBLISH_ENTITY = "publishEntity";
    private RedwineDetailResponse.DataEntity.PublishEntity publishEntity;
    @ViewInject(R.id.supplier_next_et_vendor_name)
    private EditText mSupplierNextEtVendorName;
    @ViewInject(R.id.supplier_next_et_real_name)
    private EditText mSupplierNextEtRealName;
    @ViewInject(R.id.supplier_next_et_id_number)
    private EditText mSupplierNextEtIdNumber;
    @ViewInject(R.id.supplier_next_et_license_number)
    private EditText mSupplierNextEtLicenseNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_for_supplier_next);
        ViewUtils.inject(this);
        EventBus.getDefault().register(this);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (null != intent) {
            publishEntity = (RedwineDetailResponse.DataEntity.PublishEntity) intent.getSerializableExtra(PUBLISH_ENTITY);
        }
        headerTitle.setText(R.string.supplier_certification);
        layoutBack.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.layout_back, R.id.supplier_next_tv_confirm_to_apply_for})
    public void mClick(View v) {
        switch (v.getId()) {

            case R.id.layout_back:
                finish();
                break;

            case R.id.supplier_next_tv_confirm_to_apply_for:
                submit();
                break;
        }
    }

    private void submit() {
        String vendorName = mSupplierNextEtVendorName.getText().toString();
        if (TextUtils.isEmpty(vendorName)) {
            Utils.showToast("商家名称不能为空！");
            return;
        }
        String realName = mSupplierNextEtRealName.getText().toString();
        if (TextUtils.isEmpty(realName)) {
            Utils.showToast("真实姓名不能为空！");
            return;
        }
        String idNumber = mSupplierNextEtIdNumber.getText().toString();
        if (TextUtils.isEmpty(idNumber)) {
            Utils.showToast("身份证号码不能为空！");
            return;
        } else if (!isLegalId(idNumber)) {
            Utils.showToast("身份证号码输入有误！");
            return;
        }

        String licenseName = mSupplierNextEtLicenseNumber.getText().toString();
        if (TextUtils.isEmpty(licenseName)) {
            Utils.showToast("营业执照号不能为空！");
            return;
        }

        publishEntity.setVendorName(vendorName);
        publishEntity.setRealName(realName);
        publishEntity.setIdNumber(idNumber);
        publishEntity.setLicenseNumber(licenseName);
        Intent intent = new Intent(ApplyForSupplierNextActivity.this, ApplyForSupplierLastActivity.class);
        intent.putExtra(ApplyForSupplierLastActivity.PUBLISH_ENTITY, publishEntity);
        startActivity(intent);
    }

    public void onEventMainThread(SendMsgEvent event) {
        if (event.getSendType() == SendMsgEvent.CLOSE_ACTIVITY) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 验证输入的身份证号是否合法
     */
    public static boolean isLegalId(String id) {
        if (id.toUpperCase().matches("(^\\d{15}$)|(^\\d{17}([0-9]|X)$)")) {
            return true;
        } else {
            return false;
        }
    }
}
